class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        vector<int> res;
        multiset<int> hash;
        for (int l = 0, r = 0; r < nums.size(); r++) 
        {
            hash.insert(nums[r]);
            if (r - l + 1 == k)
            {
                res.push_back(*hash.rbegin());
                hash.erase(hash.find(nums[l++]));
            }
        }
        return res;
    }
};

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        vector<int> res;
        priority_queue<pair<int, int>> pq;
        for (int i = 0; i < k; i++)
        {
            pq.emplace(nums[i], i);
        }
        res.push_back(pq.top().first);
        for (int i = k; i < nums.size(); i++)
        {
            pq.emplace(nums[i], i);
            while (pq.top().second <= i - k) // 关键 <=
            {
                pq.pop();
            } 
            res.push_back(pq.top().first);
        } 
        return res;
    }
};